package com.my.study.config;

import com.my.study.quartz.JobFactory;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import java.io.IOException;
import java.util.Properties;

/**
 * @author: yidujun
 * @create: 2021/04/29 10:15
 */
@Configuration
public class QuartzConfig {

    @Autowired
    private JobFactory jobFactory;

    @Bean
    public SchedulerFactoryBean schedulerFactoryBean() {
        SchedulerFactoryBean bean = new SchedulerFactoryBean();
        try {
            bean.setOverwriteExistingJobs(true);
//            bean.setQuartzProperties(quartzProperties());
            bean.setJobFactory(jobFactory);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return bean;
    }


//    @Bean
//    public Properties quartzProperties() throws IOException {
//        PropertiesFactoryBean bean = new PropertiesFactoryBean();
//        bean.setLocation(new ClassPathResource("/config/quartz.properties"));
//        bean.afterPropertiesSet();
//        return bean.getObject();
//    }

    @Bean(name = "scheduler")
    public Scheduler scheduler() {
        return schedulerFactoryBean().getScheduler();
    }

}
